﻿using System.Collections.Generic;
using System.Linq;
using ZQNB.BaseLib.Core.Models;
using ZQNB.Common.Encrypt;
using ZQNB.Common.FileDbs;

namespace ZQNB.BaseLib.Core.Services
{
    public class SuperAccountService : ISuperAccountService
    {
        private readonly IFileDbHelper _fileDbHelper;
        private readonly PasswordEncryptor _passwordEncryptor;

        public SuperAccountService(IFileDbHelper fileDbHelper, PasswordEncryptor passwordEncryptor)
        {
            _fileDbHelper = fileDbHelper;
            _passwordEncryptor = passwordEncryptor;
        }

        public IList<string> GetSuperAccounts()
        {
            var theOnes = _fileDbHelper.Read<SuperAccount>().Select(x => x.Username).ToList();
            return theOnes;
        }

        public bool ValidatePassword(SuperAccount account)
        {
            var theOnes = _fileDbHelper.Read<SuperAccount>();

            return theOnes.Any(x => x.Username == account.Username
                                             && x.Password == _passwordEncryptor.Encrypt(account.Password));
        }
    }
}
